package players;

import bin.Mud;
import items.Item;
import items.ItemType;
import items.SlotType;
import items.armor.Armor;
import items.weapons.FoamSword;

import java.util.ArrayList;
import java.util.List;

import exceptions.MudExceptions;

public class TBurton implements Player {
    private Mud mudWindow;
    private List<Item> inventory = new ArrayList<Item>();
    private List<Item> equipment = new ArrayList<Item>();
    private Item rightHandSlot = null;
    private Item leftHandSlot = null;
    private Armor headSlot = null;
    private Armor chestSlot = null;
    private Armor legsSlot = null;
    private String nothing = "Nothing";
    private int healthPoints = 100;
    private int maxHP = 100;

    public TBurton(Mud mudWindow) {
        this.mudWindow = mudWindow;
        inventory.add(new FoamSword());
    }

    public String getUserName() {
        return "TBurton";
    }

    @Override
    public String getDescription() {
        ImportPlayerFile input = new ImportPlayerFile(mudWindow);
        input.getImage();
        return input.getFile();
    }    

    public Integer getLevel() {
        return 1;
    }

    public Integer getHealthPoints() {
        return healthPoints;
    }

    public void setHealthPoints(int hp) {
        if (hp <= getMaximumHealth()) {
            healthPoints = hp;
        } else {
            healthPoints = getMaximumHealth();
        }
        mudWindow.showHealthPoints(hp, maxHP);
    }

    @Override
    public void setMaximumHealth(int newMax) {
        maxHP = newMax;
    }

    @Override
    public int getMaximumHealth() {
        return maxHP;
    }

    public int getBaseAttack() {
        return 12;
    }

    public int getModifiedAttack() {
        return getBaseAttack();
    }

    public List<Item> getPlayerInventory(ItemType itemType) {
        if (itemType == null) {
            return inventory;
        }

        List<Item> filteredInventory = new ArrayList<Item>();
        for (Item item : inventory) {
            if (item.getItemType() == itemType) {
                filteredInventory.add(item);
            }
        }
        return filteredInventory;
    }

    public List<Item> getPlayerEquipment() {
        return equipment;
    }

    public String getPlayerInventoryString() {
        StringBuilder buf = new StringBuilder();
        for (Item item : inventory) {
            buf.append("\n");
            buf.append(item.getTitle());
            buf.append("(");
            buf.append(item.getShortName());
            buf.append(")");
        }
        return buf.toString();
    }

    public void addItemToInventory(Item item) {
        inventory.add(item);
    }

    public void removeItemFromInventory(Item item) {
        inventory.remove(item);
    }

    public void equipItemAndRemoveFromInventory(Item item) {
        equipment.add(item);
        inventory.remove(item);
    }

    public void unequipItemAndAddToInventory(Item item) {
        inventory.add(item);
        equipment.remove(item);
    }

    public String getEquipText(Item item) {
        String s;
        if (item.getItemType().equals(ItemType.WEAPON)) {
            s = "You wield your " + item.getTitle() + ".";
        } else {
            s = "You get your " + item.getTitle() + " on";
        }
        return s;
    }

    public void equipItem(Item item) {
        mudWindow.setReturnText(getEquipText(item));

        if (item.getSlotType() == SlotType.RIGHT_HAND && rightHandSlot == null) {
            rightHandSlot = item;
            equipItemAndRemoveFromInventory(item);
        } else if (item.getSlotType() == SlotType.LEFT_HAND && leftHandSlot == null) {
            leftHandSlot = item;
            equipItemAndRemoveFromInventory(item);
        } else if (item.getSlotType() == SlotType.HEAD && headSlot == null) {
            headSlot = ((Armor) item);
            equipItemAndRemoveFromInventory(item);
        } else if (item.getSlotType() == SlotType.CHEST && chestSlot == null) {
            chestSlot = ((Armor) item);
            equipItemAndRemoveFromInventory(item);
        } else if (item.getSlotType() == SlotType.LEGS && legsSlot == null) {
            legsSlot = ((Armor) item);
            equipItemAndRemoveFromInventory(item);
        } else {
            throw MudExceptions.invalidSlot();
        }
    }

    public void removeItemFromEquipment(Item item) {
        mudWindow.setReturnText("You remove your " + item.getTitle());
        if (item.getSlotType() == SlotType.RIGHT_HAND && rightHandSlot.equals(item)) {
            rightHandSlot = null;
            unequipItemAndAddToInventory(item);
        } else if (item.getSlotType() == SlotType.LEFT_HAND && leftHandSlot.equals(item)) {
            leftHandSlot = null;
            unequipItemAndAddToInventory(item);
        } else if (item.getSlotType() == SlotType.HEAD && headSlot.equals(item)) {
            headSlot = null;
            unequipItemAndAddToInventory(item);
        } else if (item.getSlotType() == SlotType.CHEST && chestSlot.equals(item)) {
            chestSlot = null;
            unequipItemAndAddToInventory(item);
        } else if (item.getSlotType() == SlotType.LEGS && legsSlot.equals(item)) {
            legsSlot = null;
            unequipItemAndAddToInventory(item);
        } else {
            throw MudExceptions.invalidSlot();
        }
    }

    public void showEquipment() {
        mudWindow.setReturnText("Your equipment:\n\n" +
                "Head:  " + getHeadSlotString() + "\n" +
                "Chest:  " + getChestSlotString() + "\n" +
                "Legs:  " + getLegsSlotString() + "\n" +
                "Right Hand:  " + getRightHandSlotString() + "\n" +
                "Left Hand:  " + getLeftHandSlotString() + "\n");
    }

    private String getStatsScale(int value, StatType stat) {
            String s ="";
            if (value <= 10) {
                s = "You have Crappy " + stat + " skills";
            } else if (value > 10 && value <= 50) {
                s = "You have Bumbling " + stat + " skills";
            } else if (value > 50 && value <= 100) {
                s = "You have Haxxors " + stat + " skills";
            } else if (value > 100 && value <= 200) {
                s = "You have Adequate " + stat + " skills";
            } else if (value > 200) {
                s = "You have teh 1337 " + stat + " skills";
            }
        return s;
    }
    
    @Override
    public void showStats() {
        String hackSkills = getStatsScale(getHaxxors(), StatType.HAXXORS);
        String masterySkills = getStatsScale(getMastery(), StatType.MASTERY);
        String confSkills = getStatsScale(getConfucius(), StatType.CONFUCIUS);
        String awesomeSkills = getStatsScale(getAwesomeness(), StatType.AWESOMENESS);


        mudWindow.setReturnText("Your stats:\n\n" +
                "Armor" + "(" + getArmor() + ")\n" +
                hackSkills + "(" + getHaxxors() + ")\n" +
                masterySkills + "(" + getMastery() + ")\n" +
                confSkills + "(" + getConfucius() + ")\n" +
                awesomeSkills + "(" + getAwesomeness() + ")\n");
    }

    @Override
    public void die() {
        mudWindow.setReturnText("You have died.");
    }

    @Override
    public int getArmor() {
        int head = (headSlot != null) ? headSlot.getArmorModifier() : 0;
        int chest = (chestSlot != null) ? chestSlot.getArmorModifier() : 0;
        int legs = (legsSlot != null) ? legsSlot.getArmorModifier() : 0;

        return head + chest + legs;
    }

    @Override
    public int getAwesomeness() {
        int head = (headSlot != null) ? headSlot.getAwesomenessModifier() : 0;
        int chest = (chestSlot != null) ? chestSlot.getAwesomenessModifier() : 0;
        int legs = (legsSlot != null) ? legsSlot.getAwesomenessModifier() : 0;
        int right = (rightHandSlot != null) ? rightHandSlot.getAwesomenessModifier() : 0;
        int left = (leftHandSlot != null) ? leftHandSlot.getAwesomenessModifier() : 0;

        return head + chest + legs + right + left;
    }

    @Override
    public int getConfucius() {
        int head = (headSlot != null) ? headSlot.getConfuciusModifier() : 0;
        int chest = (chestSlot != null) ? chestSlot.getConfuciusModifier() : 0;
        int legs = (legsSlot != null) ? legsSlot.getConfuciusModifier() : 0;
        int right = (rightHandSlot != null) ? rightHandSlot.getConfuciusModifier() : 0;
        int left = (leftHandSlot != null) ? leftHandSlot.getConfuciusModifier() : 0;

        return head + chest + legs + right + left;
    }

    @Override
    public int getMastery() {
        int head = (headSlot != null) ? headSlot.getMasteryModifier() : 0;
        int chest = (chestSlot != null) ? chestSlot.getMasteryModifier() : 0;
        int legs = (legsSlot != null) ? legsSlot.getMasteryModifier() : 0;
        int right = (rightHandSlot != null) ? rightHandSlot.getMasteryModifier() : 0;
        int left = (leftHandSlot != null) ? leftHandSlot.getMasteryModifier() : 0;

        return head + chest + legs + right + left;
    }

    @Override
    public int getHaxxors() {
        int head = (headSlot != null) ? headSlot.getHaxxorsModifier() : 0;
        int chest = (chestSlot != null) ? chestSlot.getHaxxorsModifier() : 0;
        int legs = (legsSlot != null) ? legsSlot.getHaxxorsModifier() : 0;
        int right = (rightHandSlot != null) ? rightHandSlot.getHaxxorsModifier() : 0;
        int left = (leftHandSlot != null) ? leftHandSlot.getHaxxorsModifier() : 0;

        return head + chest + legs + right + left;
    }

    public String getHeadSlotString() {
        return (headSlot != null) ? headSlot.getDisplayName() : nothing;
    }

    public String getChestSlotString() {
        return (chestSlot != null) ? chestSlot.getDisplayName() : nothing;
    }

    public String getLegsSlotString() {
        return (legsSlot != null) ? legsSlot.getDisplayName() : nothing;
    }

    public String getRightHandSlotString() {
        return (rightHandSlot != null) ? rightHandSlot.getDisplayName() : nothing;
    }

    public String getLeftHandSlotString() {
        return (leftHandSlot != null) ? leftHandSlot.getDisplayName() : nothing;
    }
}